﻿namespace FrameWork.Lib.HTML
{
    using System;
    using System.ComponentModel;
    using System.Text;

    public class HtmlDocument
    {
        private HtmlNodeCollection mNodes = new HtmlNodeCollection(null);
        private string mXhtmlHeader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";

        internal HtmlDocument(string html, bool wantSpaces)
        {
            HtmlParser parser = new HtmlParser();
            parser.RemoveEmptyElementText = !wantSpaces;
            this.mNodes = parser.Parse(html);
        }

        public static HtmlDocument Create(string html)
        {
            return new HtmlDocument(html, false);
        }

        public static HtmlDocument Create(string html, bool wantSpaces)
        {
            return new HtmlDocument(html, wantSpaces);
        }

        [Description("This is the DOCTYPE for XHTML production"), Category("General")]
        public string DocTypeXHTML
        {
            get
            {
                return this.mXhtmlHeader;
            }
            set
            {
                this.mXhtmlHeader = value;
            }
        }

        [Description("The HTML version of this document"), Category("Output")]
        public string HTML
        {
            get
            {
                StringBuilder builder = new StringBuilder();
                foreach (HtmlNode node in this.Nodes)
                {
                    builder.Append(node.HTML);
                }
                return builder.ToString();
            }
        }

        public HtmlNodeCollection Nodes
        {
            get
            {
                return this.mNodes;
            }
        }

        [Description("The XHTML version of this document"), Category("Output")]
        public string XHTML
        {
            get
            {
                StringBuilder builder = new StringBuilder();
                if (this.mXhtmlHeader != null)
                {
                    builder.Append(this.mXhtmlHeader);
                    builder.Append("\r\n");
                }
                foreach (HtmlNode node in this.Nodes)
                {
                    builder.Append(node.XHTML);
                }
                return builder.ToString();
            }
        }
    }
}

